# Linux - awk命令详解 https://blog.csdn.net/qq_48391148/article/details/125602640

#awk命令
# -F	指定分隔符
# -v	指定变量和默认值
# $NF	代表最后一个字段
# NR	代表第几行
# &&	与
# ||	或
# FS	输入分隔符，与-F分隔符一样
# OFS	输出字段分隔符
# RS	输入记录分隔符
# $0	显示整行
# 1...N	第一个字段到第N个字段

# 获得IP
#ip=echo $SSH_CONNECTION | awk '{print $3}' # 定时任务环境下, 不能获得ip，ip为空
# $ip
#172.16.192.154

#获得日期
# root@ubuntu:/var/log# echo `date +%Y%m%d`
# 20240223
# root@ubuntu:/var/log# echo $(date +%Y%m%d)
# 20240223

# root@ubuntu:/var/log# echo `date +%Y-%m-%d[%H:%M:%S]`
# 2024-02-23[06:59:47]
# root@ubuntu:/var/log# echo $(date +%Y-%m-%d[%H:%M:%S])
# 2024-02-23[07:00:35]

# /etc/get_ip.sh
# ip=`/usr/sbin/ifconfig | grep inet | awk 'NR==1{printf "%s\n", $2}'`
# echo $ip  >> /var/log/ip.log 2>&1

# AI https://aimx.cc/#/industryModel/industry/464?appId=721
# AI: shell 获得IP        --获得IP OK! 简单！
# ip=`hostname -I`
# echo $ip  >> /var/log/ip.log 2>&1

# 定时任务使用
# crontab -l
# */1 * * * * source /etc/profile; source ~/.bash_profile; /bin/bash /etc/get_ip.sh

# tail -1 /etc/passwd | awk -F ":" '{print $6}' #指定分隔符“:”并截取第六个字段./var/empty
# seq 1 10 > 1.txt #生成一个1到10的文件,我想计算出文件内的总和 
# awk -v "a=0" '{a+=$1;print a}' 1.txt | tail -1  #给a赋值为0，让a与文件内的第一个字段相加，最后结果通过tail 打印出来 但是看的出来很麻烦
# ##优先级END 下面会有讲 
# awk -v "a=0" '{a+=$1}END{print a}' 1.txt  


# root@ubuntu:~# df -h
# Filesystem      Size  Used Avail Use% Mounted on
# udev            1.9G     0  1.9G   0% /dev
# tmpfs           388M  2.0M  386M   1% /run
# /dev/sda5        20G  8.6G  9.5G  48% /
# tmpfs           1.9G     0  1.9G   0% /dev/shm
# tmpfs           5.0M  4.0K  5.0M   1% /run/lock
# tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
# /dev/loop0       56M   56M     0 100% /snap/core18/2128
# /dev/loop1      219M  219M     0 100% /snap/gnome-3-34-1804/72
# /dev/loop2      497M  497M     0 100% /snap/gnome-42-2204/141
# /dev/loop3       66M   66M     0 100% /snap/gtk-common-themes/1515
# /dev/loop4      128K  128K     0 100% /snap/bare/5
# /dev/loop5       75M   75M     0 100% /snap/core22/1033
# /dev/loop6       56M   56M     0 100% /snap/core18/2812
# /dev/loop7       51M   51M     0 100% /snap/snap-store/547
# /dev/loop9       13M   13M     0 100% /snap/snap-store/959
# /dev/loop10      92M   92M     0 100% /snap/gtk-common-themes/1535
# /dev/loop11     219M  219M     0 100% /snap/gnome-3-34-1804/93
# /dev/sda1       511M  4.0K  511M   1% /boot/efi
# tmpfs           388M   72K  388M   1% /run/user/1000
# /dev/loop12      41M   41M     0 100% /snap/snapd/20671
# /dev/loop8       75M   75M     0 100% /snap/core22/1122
# tmpfs           388M     0  388M   0% /run/user/0
# root@ubuntu:~# df -h | grep /dev/sda5 | awk '{print $2}'
# 20G

# 内在
# root@ubuntu:~# free -m
#               total        used        free      shared  buff/cache   available
# Mem:           3876        1543         212          10        2120        2036
# Swap:           923           5         917
# root@ubuntu:~# free -m | awk 'NR==2{printf  "%dM\n", $2 }'
# 3876M

# root@ubuntu:~# free -m
#               total        used        free      shared  buff/cache   available
# Mem:           3876        1782         171          18        1922        1808
# Swap:           923           1         921
# root@ubuntu:~# free -m | grep Mem | awk '{print $6}'
# 1922

# top命令 https://www.runoob.com/linux/linux-comm-top.html
# top 命令使用教程 https://blog.csdn.net/qq_19922839/article/details/120011790
# Linux（Ubuntu）之top命令  https://blog.csdn.net/WZ18810463869/article/details/82970326
# root@ubuntu:~# top -b -n 1 | grep Cpu
# %Cpu(s):  2.9 us,  5.7 sy,  0.0 ni, 91.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
# root@ubuntu:~# top -b -n 1 | grep Cpu | awk '{print $8}'
# 94.1
# root@ubuntu:~# top -b -n 1 | grep Cpu | awk '{print $8}' | cut -d "." -f 1
# 91

# root@ubuntu:~# top -b -n 1 | grep Mem
# MiB Mem :   3876.4 total,    169.8 free,   1782.5 used,   1924.1 buff/cache
# MiB Swap:    923.3 total,    922.0 free,      1.3 used.   1808.0 avail Mem 
# root@ubuntu:~# top -b -n 1 | grep Mem | awk '{print $8}' | cut -d "u" -f 1 
# 1782.5



#------------------------------------------------------
#/Users/wuchunlong/local/automation-operation-maintenance-scripts/kjyw-Ubuntu/shell/ask_for_if_file
#wuchunlong@wuchunlong ask_for_if_file$ 


# userlist.log
# wu 123
# zh 123

# 文件操作
# wuchunlong@wuchunlong ask_for_if_file$ ./awk_for_if.sh 
# wu
# 123
# zh
# 123

for name in $(cat userlist.log | awk -F ' ' '{print $1,$2}')
do
	if [ -n $name ] 
	then 
		echo $name 
	else 
		echo "user is null"
	fi
done
